//
//  LadingBillDetailVC.m
//  ReceiptDelivery
//
//  Created by 吕晶 on 2020/7/7.
//  Copyright © 2020 吕晶. All rights reserved.
//

#import "LadingBillDetailVC.h"
#import "LadingBillDetailCell.h"
#import "RejectOperationView.h"
#import "LJInPutTool.h"
#import "LadingBillMainModel.h"
#import "AddLadingBillVC.h"
#import "GoodsListVC.h"
#import "LadingCarMapVC.h"

@interface LadingBillDetailVC ()<UITableViewDelegate,UITableViewDataSource>

@property (nonatomic, strong) UITableView *tableView;
@property (nonatomic, strong) LadingBillMainModel *billModel;
@property (nonatomic, strong) RejectOperationView *topView;
@property (nonatomic, strong) UIView *bottomView;
@property (nonatomic, strong) LJInPutTool *inPutTool;//输入框

@end

@implementation LadingBillDetailVC

-(LJInPutTool *)inPutTool {
    if (!_inPutTool) {
        _inPutTool = [LJInPutTool new];
        __weak_self;
        _inPutTool.finishBlock = ^(NSString * _Nonnull str) {
            if (weakself.billModel.status == LadingBillStatusModeNew) {
                //新建状态下，驳回
                [weakself boHuiBill:str];
            }else if (weakself.billModel.status == LadingBillStatusModeBoHui) {
                //驳回状态下，作废
                [weakself zuoFeiBill:str];
            }
        };
    }
    return _inPutTool;
}

- (void)boHuiBill:(NSString *)str {
    [HttpRequest PostWithApi:ReceiptDeliveryBoHuiLadingBill params:@{@"dp_id":self.dp_id,@"reject":str} progress:^(NSProgress * _Nonnull progress) {
        
    } success:^(NSURLSessionDataTask * _Nonnull operation, id  _Nonnull responseObject, BOOL code, NSInteger errorCode) {
        if (code) {
            [MBProgressHUD leShowSuccessHUDAddedTo:self.view message:@"驳回成功"];
            [self requestForData];
            [[NSNotificationCenter defaultCenter] postNotificationName:ReceiptDeliveryRefreshLadingBillList object:nil];
        }
    } fail:^(NSError * _Nonnull error) {
        
    }];
}

- (void)zuoFeiBill:(NSString *)str {
    [HttpRequest PostWithApi:ReceiptDeliveryZuoFeiLadingBill params:@{@"dp_id":self.dp_id,@"invalid_reason":str} progress:^(NSProgress * _Nonnull progress) {
        
    } success:^(NSURLSessionDataTask * _Nonnull operation, id  _Nonnull responseObject, BOOL code, NSInteger errorCode) {
        if (code) {
            [MBProgressHUD leShowSuccessHUDAddedTo:self.view message:@"作废成功"];
            [self requestForData];
            [[NSNotificationCenter defaultCenter] postNotificationName:ReceiptDeliveryRefreshLadingBillList object:nil];
        }
    } fail:^(NSError * _Nonnull error) {
        
    }];
}

-(RejectOperationView *)topView {
    if (!_topView) {
        _topView = [RejectOperationView new];
        __weak_self;
        _topView.modifyBlock = ^{
            //修改提货单
            AddLadingBillVC *vc = [AddLadingBillVC new];
            vc.dp_id = weakself.dp_id;
            [weakself.rt_navigationController pushViewController:vc animated:YES];
        };
        _topView.deleteBlock = ^{
            //作废
            weakself.inPutTool.placeholder = @"请输入作废原因";
            weakself.inPutTool.sureBtnText = @"确认作废";
            weakself.inPutTool.cancleBtnText = @"取消作废";
            [weakself.inPutTool showInView:MAIN_WINDOW];
        };
    }
    return _topView;
}

- (void)lookCarClick {
    LadingCarMapVC *vc = [LadingCarMapVC new];
    vc.dp_id = self.dp_id;
    [self.rt_navigationController pushViewController:vc animated:YES];
}

-(UIView *)bottomView {
    if (!_bottomView) {
        _bottomView = [[UIView alloc] initWithFrame:CGRectMake(0, ScreenH - BottomHeight - NaviHeight - 64, ScreenW, 64+BottomHeight)];
        _bottomView.backgroundColor = HexColor(0xffffff);
    }
    return _bottomView;
}

- (void)rejectClick {
    self.inPutTool.placeholder = @"请输入驳回原因";
    self.inPutTool.sureBtnText = @"确认驳回";
    self.inPutTool.cancleBtnText = @"取消驳回";
    [self.inPutTool showInView:MAIN_WINDOW];
}

- (void)passClick {
    UIAlertController *alert = [UIAlertController alertControllerWithTitle:nil message:@"确定通过提货单吗？" preferredStyle:UIAlertControllerStyleAlert];
    UIAlertAction *sureAction = [UIAlertAction actionWithTitle:@"确定" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
        [self sureBill:self.dp_id];
    }];
    UIAlertAction *cancleAction = [UIAlertAction actionWithTitle:@"取消" style:UIAlertActionStyleCancel handler:^(UIAlertAction * _Nonnull action) {
        
    }];
    [alert addAction:sureAction];
    [alert addAction:cancleAction];
    [self presentViewController:alert animated:YES completion:nil];
}

- (void)sureBill:(NSString *)dp_id {
    [HttpRequest PostWithApi:ReceiptDeliverySureLadingBill params:@{@"dp_id":dp_id} progress:^(NSProgress * _Nonnull progress) {
        
    } success:^(NSURLSessionDataTask * _Nonnull operation, id  _Nonnull responseObject, BOOL code, NSInteger errorCode) {
        if (code) {
            [MBProgressHUD leShowSuccessHUDAddedTo:self.view message:@"确认成功"];
            [self requestForData];
            [[NSNotificationCenter defaultCenter] postNotificationName:ReceiptDeliveryRefreshLadingBillList object:nil];
        }
    } fail:^(NSError * _Nonnull error) {
        
    }];
}

-(LadingBillMainModel *)billModel {
    if (!_billModel) {
        _billModel = [LadingBillMainModel new];
    }
    return _billModel;
}

-(UITableView *)tableView {
    if (!_tableView) {
        _tableView = [[UITableView alloc] initWithFrame:CGRectMake(0, self.topView.nim_bottom, ScreenW, ScreenH - NaviHeight - self.topView.nim_height - self.bottomView.nim_height)];
        _tableView.backgroundColor = Color_backgroundColor;
        _tableView.delegate = self;
        _tableView.dataSource = self;
        _tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
        __weak_self;
        _tableView.mj_header = [MJRefreshNormalHeader headerWithRefreshingBlock:^{
            [weakself requestForData];
        }];
    }
    return _tableView;
}

- (void)viewDidLoad {
    [super viewDidLoad];
    self.title = @"提货单详情";
    [self.view addSubview:self.tableView];
    [self requestForData];
}

- (void)configUI {
    if (self.billModel.status == LadingBillStatusModeNew) {
        if (self.billModel.dp_type == 1) {
            //3pl发布的新建，待确认
            [self.view addSubview:self.bottomView];
            UIButton *rejectBtn = [[UIButton alloc] initWithFrame:CGRectMake(30, 10, (_bottomView.nim_width - 15 - 60) * 0.5, 44)];
            [rejectBtn setBackgroundColor:HexColor(0xF35959)];
            rejectBtn.layer.masksToBounds = YES;
            rejectBtn.layer.cornerRadius = 6;
            [rejectBtn setImage:[UIImage imageNamed:@"icon_reject"] forState:0];
            [rejectBtn setTitle:@"驳回" forState:0];
            [rejectBtn setTitleColor:HexColor(0xffffff) forState:0];
            rejectBtn.titleLabel.font = BoldFontOfSize(16);
            [rejectBtn addTarget:self action:@selector(rejectClick) forControlEvents:UIControlEventTouchUpInside];
            rejectBtn.imageEdgeInsets = UIEdgeInsetsMake(0, -10, 0, 0);
            [self.bottomView addSubview:rejectBtn];
            
            UIButton *passBtn = [[UIButton alloc] initWithFrame:CGRectMake(rejectBtn.nim_right + 15, 10, (_bottomView.nim_width - 15 - 60) * 0.5, 44)];
            [passBtn setBackgroundColor:Color_ThemeColor];
            passBtn.layer.masksToBounds = YES;
            passBtn.layer.cornerRadius = 6;
            [passBtn setImage:[UIImage imageNamed:@"icon_adopt"] forState:0];
            [passBtn setTitle:@"通过" forState:0];
            [passBtn setTitleColor:HexColor(0xffffff) forState:0];
            passBtn.titleLabel.font = BoldFontOfSize(16);
            [passBtn addTarget:self action:@selector(passClick) forControlEvents:UIControlEventTouchUpInside];
            passBtn.imageEdgeInsets = UIEdgeInsetsMake(0, -10, 0, 0);
            [self.bottomView addSubview:passBtn];
            
            [self.topView removeFromSuperview];
            self.tableView.frame = CGRectMake(0, 0, ScreenW, ScreenH - NaviHeight - self.bottomView.nim_height);
        }else {
            //我发布的新建，显示tableview
            [self.topView removeFromSuperview];
            [self.bottomView removeFromSuperview];
            self.tableView.frame = CGRectMake(0, 0, ScreenW, ScreenH - NaviHeight - BottomHeight);
        }
    }else if (self.billModel.status == LadingBillStatusModeBoHui) {
        if (self.billModel.dp_type == 1) {
            //3pl发布的驳回，显示驳回原因
            [self.bottomView removeFromSuperview];
            [self.view addSubview:self.topView];
            self.topView.canOperation = NO;
            self.topView.frame = CGRectMake(0, 0, ScreenW, _topView.height);
            self.topView.titleStr = @"您的提货单审核未通过！ ";
            self.topView.timeStr = self.billModel.operationTime;
            self.topView.descStr = self.billModel.reject_remarks;
            self.tableView.frame = CGRectMake(0, self.topView.nim_bottom, ScreenW, ScreenH - NaviHeight - BottomHeight - self.topView.nim_height);
        }else {
            //我发布的驳回，显示驳回原因，并可以操作
            [self.bottomView removeFromSuperview];
            [self.view addSubview:self.topView];
            self.topView.canOperation = YES;
            self.topView.frame = CGRectMake(0, 0, ScreenW, _topView.height);
            self.topView.titleStr = @"您的提货单审核未通过！ ";
            self.topView.timeStr = self.billModel.operationTime;
            self.topView.descStr = self.billModel.reject_remarks;
            self.tableView.frame = CGRectMake(0, self.topView.nim_bottom, ScreenW, ScreenH - NaviHeight - BottomHeight - self.topView.nim_height);
        }
    }else {
        //其他情况，显示查看路线按钮
        [self.topView removeFromSuperview];
        [self.view addSubview:self.bottomView];
        UIButton *nextBtn = [[UIButton alloc] initWithFrame:CGRectMake(30, 10, _bottomView.nim_width - 60, 44)];
        [nextBtn setBackgroundColor:Color_ThemeColor];
        nextBtn.layer.masksToBounds = YES;
        nextBtn.layer.cornerRadius = 6;
        [nextBtn setImage:[UIImage imageNamed:@"icon_location"] forState:0];
        [nextBtn setTitle:@"查看提货车辆信息" forState:0];
        [nextBtn setTitleColor:HexColor(0xffffff) forState:0];
        nextBtn.titleLabel.font = BoldFontOfSize(16);
        [nextBtn addTarget:self action:@selector(lookCarClick) forControlEvents:UIControlEventTouchUpInside];
        [self.bottomView addSubview:nextBtn];
        self.tableView.frame = CGRectMake(0, 0, ScreenW, ScreenH - NaviHeight - self.bottomView.nim_height);
    }
}

- (void)requestForData {
    [HttpRequest GetWithApi:ReceiptDeliveryLadingBillDetail params:@{@"dp_id":self.dp_id} progress:^(NSProgress * _Nonnull progress) {
        
    } success:^(NSURLSessionDataTask * _Nonnull operation, id  _Nonnull responseObject, BOOL code, NSInteger errorCode) {
        if (code) {
            self.billModel = [LadingBillMainModel mj_objectWithKeyValues:responseObject];
            [self configUI];
            [self.tableView reloadData];
        }
        [self.tableView.mj_header endRefreshing];
    } fail:^(NSError * _Nonnull error) {
        [self.tableView.mj_header endRefreshing];
    }];
}

-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    return 1;
}

-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    LadingBillDetailCell *cell = [LadingBillDetailCell LadingBillDetailCellWithTableView:tableView];
    cell.model = self.billModel;
    __weak_self;
    cell.lookBlock = ^{
        GoodsListVC *vc = [GoodsListVC new];
        vc.dp_id = weakself.dp_id;
        [weakself.rt_navigationController pushViewController:vc animated:YES];
    };
    return cell;
}

-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
    LadingBillDetailCell *cell = [LadingBillDetailCell LadingBillDetailCellWithTableView:tableView];
    cell.model = self.billModel;
    return cell.height;
}

@end
